package lumis.portal.sendmail.dao.hibernate;

import java.io.UnsupportedEncodingException;

import lumis.portal.sendmail.IMailConfig;
import lumis.util.security.Base64;

/**
 * Implementation of {@link IMailConfig} used by {@link SendMailDaoHib}.
 * 
 * @version $Revision: 11394 $ $Date: 2010-04-08 17:16:31 -0300 (Thu, 08 Apr 2010) $
 * @since 4.0.4
 */
class MailConfig implements IMailConfig
{
	private String id;
	private String smtpHost = "smtp";
	private int smtpPort = 25;
	private String smtpUser;
	private String smtpPassword;
	private String cryptographicProtocol = CryptographicProtocol.NONE.toString();
	private int socketTimeout = 0;
	private int socketConnectionTimeout = 0;
	private String defaultFromAddress = "default_from_address@localhost";
	private int sendInterval;
	private int maxSendThreads = 1;
	private int blockSize;
	private int blockInterval;
	private boolean removeMailsSent = false;
	private int queueLockTimeout = 300; // 5 minutes default
	private String mailSenderClass = "lumis.portal.sendmail.smtp.SmtpMailSender";
	
	public String getMailSenderClass()
	{
		return mailSenderClass;
	}
	public void setMailSenderClass(String mailSenderClass)
	{
		this.mailSenderClass = mailSenderClass;
	}
	public int getQueueLockTimeout()
	{
		return queueLockTimeout;
	}
	public void setQueueLockTimeout(int queueLockTimeout)
	{
		this.queueLockTimeout = queueLockTimeout;
	}
	public boolean getRemoveMailsSent()
	{
		return removeMailsSent;
	}
	public void setRemoveMailsSent(boolean removeMailsSent)
	{
		this.removeMailsSent = removeMailsSent;
	}
	public String getId()
	{
		return id;
	}
	public void setId(String id)
	{
		this.id = id;
	}
	public int getBlockInterval()
	{
		return blockInterval;
	}
	public void setBlockInterval(int blockInterval)
	{
		this.blockInterval = blockInterval;
	}
	public int getBlockSize()
	{
		return blockSize;
	}
	public void setBlockSize(int blockSize)
	{
		this.blockSize = blockSize;
	}
	public String getDefaultFromAddress()
	{
		return defaultFromAddress;
	}
	public void setDefaultFromAddress(String defaultFromAddress)
	{
		this.defaultFromAddress = defaultFromAddress;
	}
	public int getMaxSendThreads()
	{
		return maxSendThreads;
	}
	public void setMaxSendThreads(int maxSendThreads)
	{
		this.maxSendThreads = maxSendThreads;
	}
	public int getSendInterval()
	{
		return sendInterval;
	}
	public void setSendInterval(int sendInterval)
	{
		this.sendInterval = sendInterval;
	}
	public String getSmtpHost()
	{
		return smtpHost;
	}
	public void setSmtpHost(String smtpHost)
	{
		this.smtpHost = smtpHost;
	}
	
	public CryptographicProtocol getCryptographicProtocol()
	{	
		if ((this.cryptographicProtocol != null) && (!this.cryptographicProtocol.equals("")))
		{
			return CryptographicProtocol.valueOf(this.cryptographicProtocol);
		}
		else
		{
			this.cryptographicProtocol = CryptographicProtocol.NONE.toString();
			return CryptographicProtocol.NONE;
		}
	}
	
	public void setCryptographicProtocol(CryptographicProtocol cryptographicProtocol)
	{
		if (cryptographicProtocol == null)
			throw new IllegalArgumentException("protocolName cannot be null");

		this.cryptographicProtocol = cryptographicProtocol.toString();
	}
	
	public String getSmtpPassword()
	{
		String password;
		try
		{
			if (smtpPassword != null)
				password = new String(Base64.decode(smtpPassword), "UTF-8");
			else
				password = null;
		}
		catch (UnsupportedEncodingException e)
		{
			throw new RuntimeException(e);
		}
		return password;
	}
	public void setSmtpPassword(String smtpPassword)
	{
		if (smtpPassword != null)
		{
			byte[] passwordBytes;
			try
			{
				passwordBytes = smtpPassword.getBytes("UTF-8");
			}
			catch (UnsupportedEncodingException e)
			{
				throw new RuntimeException(e);
			}
			this.smtpPassword = Base64.encode(passwordBytes);
		}
		else
		{
			this.smtpPassword = null;
		}
	}
	public int getSmtpPort()
	{
		return smtpPort;
	}
	public void setSmtpPort(int smtpPort)
	{
		this.smtpPort = smtpPort;
	}
	public String getSmtpUser()
	{
		return smtpUser;
	}
	public void setSmtpUser(String smtpUser)
	{
		this.smtpUser = smtpUser;
	}
	public int getSocketTimeout() 
	{
		return socketTimeout;
	}
	public void setSocketTimeout(int socketTimeout) 
	{
		this.socketTimeout = socketTimeout;
	}
	public int getSocketConnectionTimeout() 
	{
		return socketConnectionTimeout;
	}
	public void setSocketConnectionTimeout(int socketConnectionTimeout) 
	{
		this.socketConnectionTimeout = socketConnectionTimeout;
	}

}
